<template>
    <div class="login">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <div class="logo">
                <img src="../../assets/logo.png" height="120px" alt="" />
            </div>
            <el-form-item label="账号" prop="username">
                <el-input v-model="ruleForm.username" status-icon:true></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input v-model="ruleForm.password" show-password></el-input>
            </el-form-item>
            <el-form-item class="login_btn">
                <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
export default {
    data() {
        return {
            ruleForm: {
                username: '',
                password: ''
            },
            rules: {
                username: [
                    { message: '请输入账号', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                password: [
                    { message: '请输入密码', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.$ajax({
                        method: "post",
                        url: "/api/login",
                        params: this.ruleForm
                    }).then(res => {
                        console.log(res.data)
                        if (res.data.code == 1) {
                            this.$message({
                                message: '登录成功！',
                                type: 'success'
                            });
                            localStorage.setItem('token',res.data.token)
                            localStorage.setItem('username',res.data.username)
                            this.$router.push('/')
                        }else if(res.data.code == 2){
                            this.$message({
                                message: '登录失败，密码错误！',
                                type: 'error'
                            });
                        }else{
                            this.$message({
                                message: '登录失败，用户名或密码错误！',
                                type: 'error'
                            });
                        }

                    });
                } else {
                    this.$message.error('登录失败');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style lang="less" scoped>
.login {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: #104468;
}

.demo-ruleForm {
    position: relative;
    background: white;
    width: 480px;
    height: 200px;
    margin: 200px auto;
    padding-top: 100px;
    padding-right: 40px;
    border-radius: 10px;

    .logo {
        position: absolute;
        margin: 0 auto;
        border: 8px solid rgb(240, 237, 237);
        border-radius: 100%;
        background: white;
        top: -70px;
        left: 190px;
        width: 120px;
        height: 120px;
        overflow: hidden;
    }

    .login_btn {
        margin-left: 100px;
    }
}
</style>